package cn.ante.basic.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.lang.model.element.VariableElement;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;


/**
* @Title: 邮件发送工具类
* @Description: TODO
* @Author: Nai
* @Version: 1.0
* @Date:  2022/7/3 14:45
* @Parameters:
* @Return
*/

//@Component
public class MailUtil {
//    @Autowired
//    private JavaMailSender javaMailSender;

//    @Value("${he.receiveMail}")
//    private String receiveMail;
    private static final String USER = "594390939@qq.com"; // 发件人称号，同邮箱地址
    private static final String PASSWORD = "evpvsthtpnasbdhh"; //使用客户端授权码

    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");

            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);

            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    }

//    private static void send(String smtpHost, String password, String formMail,String toMail,String title,
//    String content,String filePath) throws Exception {
//        Properties props = new Properties();
//        props.setProperty("mail.transport.protocol", "SMTP");
//        props.setProperty("mail.host", smtpHost);
//        props.setProperty("mail.smtp.auth", "true");
//        Authenticator auth = new Authenticator(){
//           public PasswordAuthentication getPA(){
//               return new PasswordAuthentication(formMail,password);
//           }
//        };
//        Session session = Session.getInstance(props, auth);
//        Message mimeMessage = new MimeMessage(session);
//        mimeMessage.setFrom(new InternetAddress(formMail));
//        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(toMail));
//        mimeMessage.setSubject(title);
//
//        // 邮件正文
//        MimeBodyPart body = new MimeBodyPart();
//        body.setContent(content,"text/html;charset=utf-8");
//
//
//        // 准备附件
//        DataSource source = new FileDataSource(new File(filePath));
//        DataHandler handler = new DataHandler(source);
//
//        // 添加附件
//        MimeBodyPart bodyPart = new MimeBodyPart();
//        bodyPart.setDataHandler(handler);
//        bodyPart.setFileName(MimeUtility.encodeText(handler.getName()));
//
//        // 将正文和附件放到multipart
//        Multipart multipart = new MimeMultipart();
//        multipart.addBodyPart(body);
//        multipart.addBodyPart(bodyPart);
//
//        mimeMessage.setContent(multipart);
//        Transport.send(mimeMessage);
//
//    }

//// 1.创建复杂邮件对象
//    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//    public static MimeMessageHelper getHelper(String form,String title,String context,File file,String to){
//
//    }
//


